//
//  FirstViewController.m
//  Clase4Demo
//
//  Created by Santiago Fontanarrosa on 6/20/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "FirstViewController.h"
#import "WebViewController.h"

typedef enum {
    kGoogle,
    kYahoo,
    kBing,
}kSearchEngine;

@interface FirstViewController ()
@property (nonatomic,retain) NSArray *data;
@end

@implementation FirstViewController

@synthesize table = _table;
@synthesize data = _data;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"First", @"First");
        self.tabBarItem.image = [UIImage imageNamed:@"first"];
        self.data = [NSArray arrayWithObjects:@"Google",@"Yahoo",@"Bing", nil];
    }
    return self;
}
							
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

-(void)dealloc
{
    self.data = nil;
    self.table = nil;
    [super dealloc];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.data count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if(!cell){
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text = [self.data objectAtIndex:indexPath.row];

    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSString *urlString = nil;
    switch (indexPath.row) {
        case kGoogle:
            urlString = @"http://www.google.com";
            break;
        case kYahoo:
            urlString = @"http://www.yahoo.com";
            break;
        case kBing:
            urlString = @"http://wwww.bing.com";
            break;
        default:
            break;
    }
    
    WebViewController *webVC = [[WebViewController alloc] init];
    webVC.urlStr = urlString;
    [self.navigationController pushViewController:webVC animated:YES];
    [webVC release];
}

@end
